package com.lzj.controller;

import com.lzj.entity.Employee;
import com.lzj.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @autor:lzj
 * @date:2022/2/28
 */
@Controller
@RequestMapping("/admin")
public class EmployeeController {
    public static final int PAGE_SIZE=3;//一页显示多少条数据
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/searchemployees")
    public String getAllEmployees(Employee employee, @RequestParam(defaultValue = "1") int page, Model model){
        Long total = employeeService.getTotal(employee);
        if(page>(total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1)){
            page =(int)(total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1);
        }
        if(page<=0){
            page=1;
        }
        //分页查询 传入的对象    (page - 1) * PAGE_SIZE为分页开始的偏移位置实际开始位置为下一个    PAGE_SIZE为数据总条目数
        List<Employee> allEmps = employeeService.getAllEmps(employee, (page - 1) * PAGE_SIZE, PAGE_SIZE);
        System.out.println("获取到了值:"+employee.getStatus());
        model.addAttribute("emps",allEmps);//查询到的对象
        model.addAttribute("total",total);//查询到的总数
        model.addAttribute("page",page);//第几页
        model.addAttribute("employee",employee);
        model.addAttribute("status",employee.getStatus());
        model.addAttribute("pagenum",total%PAGE_SIZE==0?total/PAGE_SIZE:total/PAGE_SIZE+1);//返回总页数
        return "searchemployees";
    }

    @RequestMapping("/updateEmp/{id}")
    public String updateEmp(@PathVariable int id,Employee employee){
        int i = employeeService.updateEmp(id, "2");
        return "redirect:/admin/searchemployees?status=1";
    }

}
